package sapling.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

public class ReadConfig {

	private static Properties properties;
	
	private static OutputStream os;

	public static int TIME_LIMIT;
	public static int SLICE;
	public static int SLICE_NUM;
	public static String DISPATCHER;

	static {
		properties = new Properties();
		InputStream is = null;
		String path = Object.class.getResource("/config.properties").getPath();
		try {
			is = new FileInputStream(path);
			properties.load(is);
			os = new FileOutputStream(path);
			readConfig();
		} catch (IOException e) {
			System.out.println("配置文件不存在！");
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static String getProperty(String key) {
		return properties.getProperty(key);
	}
	
	public static void update(String key, String value) {
		properties.setProperty(key, value);
	}

	public static void store() {
		try {
			properties.store(os, "");
			readConfig();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void readConfig() {
		TIME_LIMIT = Integer.parseInt(getProperty("timeLimit"));
		SLICE = Integer.parseInt(getProperty("timeSlice"));
		SLICE_NUM = Integer.parseInt(getProperty("sliceNum"));
		DISPATCHER = getProperty("dispatcher");
	}

}
